﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ETADTools.Lib
{
  public static class FieldFormExtension
  {
    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public static string ToAspCreateControl(this Field field)
    {
      return string.Format("mobjFormDataBinder.CreateBoundControl \"{0}\"", field.FieldName);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public static string ToAspSaveParameter(this Field field)
    {
      StringBuilder result = new StringBuilder();

      result.AppendFormat("mcmdSaveForm.Parameters(\"{0}{1}\").Value = ",
        field.FieldType.ToParameterStyle(), field.FieldName);
      result.AppendFormat("mctlForm.GetControl(\"{0}\").Value", field.FieldName);

      return result.ToString();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="field"></param>
    /// <returns></returns>
    public static string ToAspRenderLayout(this Field field)
    {
      StringBuilder result = new StringBuilder();

      result.AppendFormat("<td class=\"ControlLabel\"><% mctlForm.RenderCaption \"{0}\" %></td>", field.FieldName);
      result.AppendFormat("\n      ");
      result.AppendFormat("<td><% mctlForm.RenderControl \"{0}\" %></td>", field.FieldName);

      return result.ToString();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public static string ToSqlObjectResource(this Field field)
    {
      StringBuilder result = new StringBuilder();

      result.AppendFormat("EXEC pzResource_Load @vintObjID = @intObjID, ");
      result.AppendFormat("@vstrResourceName = N'{0}', ", field.FieldName);
      result.AppendFormat("@vintSysLID = 1033, @vstrResourceDesc = NULL, ");
      result.AppendFormat("@vstrResourceText = N'{0}'", field.ResourceText);

      return result.ToString();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public static string ToSqlFieldGroup(this Field field)
    {
      StringBuilder result = new StringBuilder();

      result.AppendFormat("EXEC @intObjPrmID = sp_zObject_ID 'ObjectParameter', @intObjID, '{0}'\n", field.FieldName);

      result.AppendFormat("EXEC pzObjectParameter_Load @vintObjPrmID = @intObjPrmID, @vintObjID = @intObjID, ");
      result.AppendFormat("@vstrName = N'{0}', @vintSysLID = 1033, ", field.FieldName);
      result.AppendFormat("@vstrCaption = N'{0}', @vstrCodeFieldName = NULL, ", field.ResourceText);
      result.AppendFormat("@vintDataTypeID = {0}, ", field.FieldType.ToDataTypeID());
      result.AppendFormat("@vblnIsRequired = 0, @vblnUseSelect = 0, @vstrSelectListSP = NULL, @vstrSelectValueField = NULL, @vstrSelectTextField = NULL, @vstrSelectHideField = NULL, @vstrSelectParentValueField = NULL, @vblnUseUnitSelect = 0, @vintUTypeID = NULL, @vblnUseFind = 0, @vintFindTypeID = NULL, ");
      result.AppendFormat("@vintSortOrder = {0}, @vblnHide = 0, @vblnUseDateTimeControl = 0, @vintDateTimeCtrlID = NULL", Constant.SortOrder);

      return result.ToString();
    }
  }
}
